﻿using ModernChannel.Data;
using ModernChannel.Model.Interface;
using ModernChannel.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ModernChannel.Provider
{
    public class RegionProvider : GenericProvider, IRegionProvider
    {
        public List<Region> SearchRegions(out int totalRecord, string textSearch = "", int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<Region>();
            if (!string.IsNullOrWhiteSpace(textSearch))
                querry = querry.Where(x => x.Name.ToUpper().Contains(textSearch.ToUpper()) || x.Id.ToUpper() == textSearch.ToUpper());

            querry = querry.OrderBy(x => x.Id);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public List<Region> SearchRegions(out int totalRecord, string regionIdentifier, string regionName, int pagesize = 0, int pageindex = 0)
        {
            var querry = repository.GetQuery<Region>();
            if (!string.IsNullOrWhiteSpace(regionIdentifier))
                querry = querry.Where(x => x.Id.ToUpper() == regionIdentifier.ToUpper());

            if (!string.IsNullOrWhiteSpace(regionName))
                querry = querry.Where(x => x.Name.ToUpper().Contains(regionName.ToUpper()));

            querry = querry.OrderBy(x => x.Id);
            totalRecord = querry.Count();
            if (pagesize > 0 && pageindex >= 0)
            {
                querry = querry.Skip(pageindex * pagesize).Take(pagesize);
            }
            return querry.ToList();
        }

        public Region RetrieveRegion(string regionIdentifier)
        {
            var regionId = (regionIdentifier ?? string.Empty).ToUpper();
            return repository.FindOne<Region>(x => x.Id.ToUpper() == regionId);
        }

        public bool Add(Region record)
        {
            try
            {
                repository.Add<Region>(record);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Update(Region record)
        {
            try
            {
                var updateRecord = RetrieveRegion(record.Id);
                if (updateRecord == null) return false;
                updateRecord.Name = record.Name;
                updateRecord.SoTransferDate = record.SoTransferDate;
                updateRecord.SoTransferFlag = record.SoTransferFlag;
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(string regionId)
        {
            try
            {
                var regid = (regionId ?? string.Empty).ToUpper();
                repository.Delete<Region>(x => x.Id.ToUpper() == regid);
                repository.UnitOfWork.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
